<?php
App::uses('AppController', 'Controller');
/**
 * Branches Controller
 *
 * @property Branch $Branch
 */
class BranchesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
	    Controller::loadModel('Stage'); 
		$this->Stage->recursive = 2;
		//$stages=$this->Stage->find("list",array("order"=>"level"));
		
		$stages_branche=$this->Stage->find("all",array("order"=>"level"));
		 
		$this->set("stages_branche",$stages_branche);
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Branch->id = $id;
		if (!$this->Branch->exists()) {
			throw new NotFoundException(__('Invalid data'));
		}
		$this->set('branch', $this->Branch->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($stage_id=null,$branch_id=null) {
		//if(isset($stage_id))
		
		Controller::loadModel('Stage'); 
		$this->Stage->recursive = 1;
		//$stages=$this->Stage->find("list",array("order"=>"level"));
		
		
		
		if ($this->request->is('post')) {
		
		 if(isset($this->request->data["Branch"]["user_id"])){
		        $useId =AuthComponent::user('id');		
		        //$useId ="1";				
				$this->request->data["Branch"]["user_id"]=$useId;
				}
				
			$this->Branch->create();
			if ($this->Branch->save($this->request->data)) {
				$id=$this->Branch->id;
				$this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
		
		//$this->set("stages",$stages);
		$stages_branche=$this->Stage->find("all",array("order"=>"level"));
		$this->set("stages_branche",$stages_branche);
		
		
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Branch->id = $id;
		if (!$this->Branch->exists()) {
			throw new NotFoundException(__('Invalid branch'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		
		 if(isset($this->request->data["Branch"]["user_id"])){
		       $useId =AuthComponent::user('id');		
		        //$useId ="1";				
				$this->request->data["Branch"]["user_id"]=$useId;
				}
				
			if ($this->Branch->save($this->request->data)) {
			      $this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		} else {
			$this->request->data = $this->Branch->read(null, $id);
		}
	   
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$data=$this->Branch->find("first",array("conditions"=>array("Branch.branch_id"=>$id)));
		$this->Branch->id = $id;
		
		
		 if(!empty($data)){
		    $this->Session->setFlash(__('Data is associated'), 'default',array("class"=>"mws-form-message warning"));
			$this->redirect(array('action' => 'index'));
		 }
		
		if (!$this->Branch->exists()) {
			throw new NotFoundException(__('Invalid branch'));
		}
		if ($this->Branch->delete()) {
		       $this->savelog($id);
				
			$this->Session->setFlash(__('Data has been deleted'), 'default',array("class"=>"mws-form-message success"));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
}
